<?php
    /**
     * Created by PhpStorm.
     * 版权所有: 2019~2022 [ hhygyl ]
     * Date: 2019/7/24-11:12
     * Link: http://luckyadmin.luckyhhy.cn
     * FileName: Page.php
     * Keys: ctrl+alt+L/ctrl+s(代码格式化) ctrl+J(代码提示) ctrl+R(替换)ALT+INSERT(生成代码(如GET,SET方法,构造函数等) , 光标在类中才生效)
     * CTRL+ALT+O (优化导入的类和包 需要配置) SHIFT+F2(高亮错误或警告快速定位错误)
     * CTRL+SHIFT+Z(代码向前) CTRL+SHIFT+/ (块状注释) ctrl+shift+enter(智能完善代码 如if())
     *
     **************************************************************
     *                                                            *
     *   .=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-.       *
     *    |                     ______                     |      *
     *    |                  .-"      "-.                  |      *
     *    |                 /            \                 |      *
     *    |     _          |              |          _     |      *
     *    |    ( \         |,  .-.  .-.  ,|         / )    |      *
     *    |     > "=._     | )(__/  \__)( |     _.=" <     |      *
     *    |    (_/"=._"=._ |/     /\     \| _.="_.="\_)    |      *
     *    |           "=._"(_     ^^     _)"_.="           |      *
     *    |               "=\__|IIIIII|__/="               |      *
     *    |              _.="| \IIIIII/ |"=._              |      *
     *    |    _     _.="_.="\          /"=._"=._     _    |      *
     *    |   ( \_.="_.="     `--------`     "=._"=._/ )   |      *
     *    |    > _.="                            "=._ <    |      *
     *    |   (_/                                    \_)   |      *
     *    |                                                |      *
     *    '-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-='      *
     *                                                            *
     *                    JUST FIND YOUR BUG !                    *
     **************************************************************
     */

    namespace app\admin\controller;


    use service\StringService;
    use think\Exception;

    class Page extends SystemBase
    {


        protected $page_model;
        protected function _initialize()
        {
            parent::_initialize(); // TODO: Change the autogenerated stub
            $this->page_model=new \app\admin\model\Page();

        }


        /**
         * @return mixed
         * @author: hhygyl <hhygyl520@qq.com>
         * @name: index
         * @describe:
         */
        public function index(){

            if($this->request->isAjax()){
                $param = $this->request->param();//接收参数
                return $this->page_model->GetPage($param);
            }
            return $this->fetch();
        }


        /**
         * @return mixed
         * @throws \think\db\exception\DataNotFoundException
         * @throws \think\db\exception\ModelNotFoundException
         * @throws \think\exception\DbException
         * @author: hhygyl <hhygyl520@qq.com>
         * @name: AddContent
         * @describe:新增内容
         */
        public function AddContent(){
            if($this->request->isAjax()) {
                $data = $this->request->post();

                if(empty($data['content'])) {
                    $this->error("内容不能为空");
                }
                $data['description'] = empty($data['description']) ? StringService::str_cut(strip_tags($data['content']), 100) : $data['description'];

                try {
                    $res = $this->page_model->addData($data);
                    if($res!=false) {
                        $this->AddLogs('添加单页内容【'.$data['title'].'】');
                        $this->success("提交成功");
                    }
                    else {
                        $this->error("提交失败");
                    }
                } catch (Exception $exception) {
                    $this->error($exception->getMessage());
                }
            }
            $navigate=new \app\admin\model\Navigate();
            $this->assign("navigate",$navigate->GetList(6));
            return $this->fetch();
        }


        /***
         * @return mixed
         * @throws \think\db\exception\DataNotFoundException
         * @throws \think\db\exception\ModelNotFoundException
         * @throws \think\exception\DbException
         * @author: hhygyl <hhygyl520@qq.com>
         * @name: EditContent
         * @describe: 编辑
         */
        public function EditContent(){
            if($this->request->isAjax()) {
                $data = $this->request->post();

                if(empty($data['content'])) {
                    $this->error("内容不能为空");
                }
                $data['description'] = empty($data['description']) ? StringService::str_cut(strip_tags($data['content']), 100) : $data['description'];

                try {
                    $res = $this->page_model->editData($data);
                    if($res!=false) {
                        $this->AddLogs('编辑单页内容【'.$data['title'].'】');
                        $this->success("提交成功");
                    }
                    else {
                        $this->error("提交失败");
                    }
                } catch (Exception $exception) {
                    $this->error($exception->getMessage());
                }
            }
            $navigate=new \app\admin\model\Navigate();
            $this->assign("navigate",$navigate->GetList(6)); //查询单页模型
            $id=$this->request->param("id/d");
            $info=$this->page_model->get($id);
            $this->assign("info",$info);
            return $this->fetch();
        }

    }